package com.truyen.controller;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.truyen.model.ChapterTmpTruyenModel;
import com.truyen.model.TruyenHintModel;
import com.truyen.persistence.entity.Account;
import com.truyen.persistence.entity.BookmarkChapter;
import com.truyen.persistence.entity.ChapterTmpTruyenMa;
import com.truyen.persistence.entity.TacGia;
import com.truyen.persistence.entity.ChapterTruyenMa;
import com.truyen.persistence.entity.TruyenMa;
import com.truyen.persistence.entity.UserLiked;
import com.truyen.service.BookmarkChapterService;
import com.truyen.service.TacGiaService;
import com.truyen.service.ChapterTruyenMaService;
import com.truyen.service.ChapterTmpTruyenMaService;
import com.truyen.service.TruyenMaService;
import com.truyen.service.UserLikedService;
import com.truyen.service.UserService;
import com.truyen.utils.PagingInfo;
import com.truyen.utils.Unsigned;

@Controller
@RequestMapping("/truyen-ma")
public class MaController {
	
	@Autowired
	@Qualifier("userDetailService")
	private UserService userService;
	
	@Autowired
	private UserLikedService userLikedService;
	
	@Autowired
	private BookmarkChapterService bookmarkChapterService;
	
	@Autowired
	private TruyenMaService truyenMaService;
	
	@Autowired
	private TacGiaService tacGiaService;
	
	@Autowired
	private ChapterTruyenMaService chapterTruyenMaService;
	
	@Autowired
	private ChapterTmpTruyenMaService chapterTmpTruyenMaService;
	
	//find chapter
	@RequestMapping("/findChapter.htm")
	@ResponseBody
	public long findChapter(@RequestParam long idTruyen, @RequestParam float chapterNumber){
		if(idTruyen > 0){
			TruyenMa truyen = truyenMaService.findById(idTruyen);
			ChapterTmpTruyenMa chapter = chapterTmpTruyenMaService.findByTruyenChapterNumber(truyen,chapterNumber);
			if(chapter != null){
				return chapter.getId();
			}
		}
		return 0;
	}
	
	@RequestMapping("/hintTruyen.htm")
	@ResponseBody
	public List<TruyenHintModel> getHint(
			@RequestParam(defaultValue = "") String kwSearch,@RequestParam(defaultValue = "") String typeSearch) {
		if (kwSearch!= null && !kwSearch.equals("") && typeSearch != null && !typeSearch.equals("")) {
			List<TruyenHintModel> lstComicHintModel = TruyenHintModel.addLstComicHintModel(truyenMaService.getObjectByNameUnsigned(kwSearch, 10, typeSearch));
			return lstComicHintModel;
		}
		return null;
	}
	
	//sidebar right
	public void sideBarIndexHome(Model model){
		List<TruyenMa> lstTruyenByViewsWeek = truyenMaService.getListTruyenBy("viewsWeek",0,10);
		List<TruyenMa> lstTruyenByViewsMonth = truyenMaService.getListTruyenBy("viewsMonth",0,10);
		List<TruyenMa> lstTruyenRandomSidebar = truyenMaService.getListTruyenRandom(10);
		model.addAttribute("lstTruyenByViewsWeek", lstTruyenByViewsWeek);
		model.addAttribute("lstTruyenByViewsMonth", lstTruyenByViewsMonth);
		model.addAttribute("lstTruyenRandomSidebar", lstTruyenRandomSidebar);
	}
	
	//list random
	public void lstRandom(Model model){
		List<TruyenMa> lstTruyenRandom = truyenMaService.getListTruyenRandom(20);
		model.addAttribute("lstTruyenRandom", lstTruyenRandom);
	}
	
	//list orderby
	public void lstOrderBy(Model model, String orderByColumn,PagingInfo pInfo){
		List<TruyenMa> lstTruyen = truyenMaService.getLstByPropertyWithOrderByPaging(orderByColumn, pInfo);
		model.addAttribute("lstTruyen", lstTruyen);
	}
	
	@RequestMapping("/trang-chu.htm")
	public String home(Model model){
		List<TruyenMa> lstTruyenByViews = truyenMaService.getListTruyenBy("views",0,4);
		List<TruyenMa> lstTruyenById = truyenMaService.getListTruyenBy("id",0,14);
		sideBarIndexHome(model);
		lstRandom(model);
		model.addAttribute("lstTruyenByViews", lstTruyenByViews);
		model.addAttribute("lstTruyenById", lstTruyenById);
		return "/truyenMa/home/index";
	}
	
	//trang info
	@RequestMapping("/T{id}/{nameUnsigned}.htm")
	public String trangInfo(Model model,@PathVariable("id") Long id, @RequestParam(defaultValue="1") int curPage) {
		if(id != null && id > 0 && curPage > 0){
			TruyenMa truyen = truyenMaService.findById(id);
			if(truyen != null){
				truyenMaService.updateViews(id,truyen.getViews()+1,truyen.getViewsWeek()+1,truyen.getViewsMonth()+1);
				sideBarIndexHome(model);
				TacGia author = truyen.getTacGia();
				PagingInfo pInfoAuthor = new PagingInfo();
				pInfoAuthor.setCurrentPage(1);
				pInfoAuthor.setSizePerPage(20);
				List<TruyenMa> lstTruyenByAuthor = truyenMaService.getLstByTacGiaWithPaging(author, "id", pInfoAuthor);
				PagingInfo pInfoChapter = new PagingInfo();
				pInfoChapter.setCurrentPage(curPage);
				List<ChapterTmpTruyenMa> lstChapterTruyen = chapterTmpTruyenMaService.getByTruyenPaging(truyen,pInfoChapter);
				model.addAttribute("truyen", truyen);
				model.addAttribute("author", author);
				model.addAttribute("lstTruyenByAuthor", lstTruyenByAuthor);
				model.addAttribute("lstChapterTruyen", lstChapterTruyen);
				model.addAttribute("countChapter", pInfoChapter.getCount());
				model.addAttribute("pInfoChapter", pInfoChapter);
				//kiem tra yeu thich
				boolean flagLiked = false;
				Account account = userService.getCurrentLoggedInUser();
				if(account != null){
					UserLiked userLiked = userLikedService.findByAccount(account,truyen.getName());
					if(userLiked != null){
						flagLiked = true;
					}
				}
				model.addAttribute("flagLiked", flagLiked);
				return "/truyenMa/info/index";
			}else{
				return "redirect:trang-chu.htm";
			}
		}else{
			return "redirect:trang-chu.htm";
		}
	}
	
	//doc chapter
	@RequestMapping("/{alias}-chuong-{chapterNumber}-T{idTruyen}-C{idChapter}.htm")
	public String readChapter(Model model,
			@PathVariable("idTruyen") Long idTruyen,
			@PathVariable("idChapter") Long idChapter,
			@PathVariable("alias") String alias,
			@PathVariable("chapterNumber") float chapterNumber){
		if(idChapter != null && idChapter > 0 && idTruyen != null && idTruyen > 0 && alias != null && !alias.equals("") && chapterNumber > 0){
			//loai bo dau - thay bang khoang trong
			TruyenMa truyen = truyenMaService.findById(idTruyen);
			if(truyen != null){
				//lay doi tuong chapter boi ten khong dau
				ChapterTruyenMa chapterTruyen = chapterTruyenMaService.findById(idChapter);
				if(chapterTruyen!=null){
					truyenMaService.updateViews(truyen.getId(),truyen.getViews()+1,truyen.getViewsWeek()+1,truyen.getViewsMonth()+1);
					//list chapter
					List<ChapterTmpTruyenModel> lstChapterTruyen = ChapterTmpTruyenModel.addLstaddChapterTruyenModel(chapterTmpTruyenMaService.getByIdTruyen(idTruyen));
					//list truyen random
					lstRandom(model);
					//list new chapter
					List<ChapterTmpTruyenMa> lstNewChapterTruyen = chapterTmpTruyenMaService.getListNewChapter(19);
					//tra ve view
					model.addAttribute("truyen", truyen);
					model.addAttribute("chapterTruyen", chapterTruyen);
					model.addAttribute("lstChapterTruyen", lstChapterTruyen);
					model.addAttribute("truyen", truyen);
					model.addAttribute("lstNewChapterTruyen", lstNewChapterTruyen);
					//kiem tra bookmark
					boolean flagGhiNho = false;
					Account account = userService.getCurrentLoggedInUser();
					if(account != null){
						BookmarkChapter bookmarkChapter = bookmarkChapterService.findByAccountIdTruyenIdChapter(account,idTruyen,idChapter);
						if(bookmarkChapter != null){
							flagGhiNho = true;
						}
					}
					model.addAttribute("flagGhiNho", flagGhiNho);
					return "/truyenMa/detail/index";
				}else{
					return "redirect:trang-chu.htm";
				}
			}else{
				return "redirect:trang-chu.htm";
			}
		}else{
			return "redirect:trang-chu.htm";
		}
	}
	
	//trang truyen theo alphabet
	@RequestMapping("/danh-sach-truyen-theo-van-{alphabet}-trang-{curPage}.htm")
	public String timKiemTheoChuCai(Model model,@PathVariable("alphabet") String alphabet, @PathVariable("curPage") int curPage) {
		if(alphabet != null && !alphabet.equals("") && curPage > 0){
			alphabet = alphabet.replace("script", "");
			sideBarIndexHome(model);
			PagingInfo pInfo = new PagingInfo();
			pInfo.setCurrentPage(curPage);
			pInfo.setSizePerPage(7);
			List<TruyenMa> lstTruyen = truyenMaService.getLstByPropertyWithPaging("alias", alphabet, "id", pInfo);
			model.addAttribute("lstTruyen", lstTruyen);
			model.addAttribute("pInfo", pInfo);
			model.addAttribute("alphabet", alphabet);
			model.addAttribute("countRecord", pInfo.getCount());
			return "/truyenMa/alphabetSearch/index";
		}else{
			return "redirect:trang-chu.htm";
		}
	}
	
	//trang truyen theo tac gia
	@RequestMapping("/tac-gia-{author}-TG{authorId}-trang-{curPage}.htm")
	public String timKiemTheoTacGia(Model model,@PathVariable("authorId") Long authorId, @PathVariable("curPage") int curPage) {
		if(authorId != null && authorId > 0 && curPage > 0){
			TacGia author = tacGiaService.findById(authorId);
			if(author != null){
				sideBarIndexHome(model);
				PagingInfo pInfo = new PagingInfo();
				pInfo.setCurrentPage(curPage);
				pInfo.setSizePerPage(7);
				List<TruyenMa> lstTruyen = truyenMaService.getLstByTacGiaWithPaging(author, "id", pInfo);
				model.addAttribute("author", author);
				model.addAttribute("lstTruyen", lstTruyen);
				model.addAttribute("pInfo", pInfo);
				model.addAttribute("countRecord", pInfo.getCount());
				return "/truyenMa/searchAuthor/index";
			}else{
				return "redirect:trang-chu.htm";
			}
		}else{
			return "redirect:trang-chu.htm";
		}
	}
	
	//trang truyen xem theo sap xep
	@RequestMapping("/danh-sach-truyen-{orderBy}-trang-{curPage}.htm")
	public String truyenXemNhieuNhat(Model model,@PathVariable("orderBy") String orderBy, @PathVariable("curPage") int curPage) {
		if(curPage > 0){
			sideBarIndexHome(model);
			PagingInfo pInfo = new PagingInfo();
			pInfo.setCurrentPage(curPage);
			pInfo.setSizePerPage(7);
			if(orderBy.equals("xem-nhieu-nhat")){
				lstOrderBy(model,"views",pInfo);
				model.addAttribute("rsOrderBy", "xem nhiều nhất");
			}else{
				lstOrderBy(model,"id",pInfo);
				model.addAttribute("rsOrderBy", "mới cập nhật");
			}
			model.addAttribute("orderBy", orderBy);
			model.addAttribute("pInfo", pInfo);
			return "/truyenMa/rsOrderBy/index";
		}else{
			return "redirect:trang-chu.htm";
		}
	}
	
	//trang tim kiem
	@RequestMapping("/tim-kiem-truyen.htm")
	public String timKiemTruyen(Model model,
			@RequestParam(defaultValue="") String k,
			@RequestParam(defaultValue = "") String typeSearch,
			@RequestParam(defaultValue="1") int p) {
		if(k != null && !k.equals("") && typeSearch != null && !typeSearch.equals("") && p > 0){
			model.addAttribute("k", k);
			model.addAttribute("typeSearch", typeSearch);
			k = k.replace("script", "");
			k = Unsigned.boDau(k);
			model.addAttribute("kUnsigned", k);
			sideBarIndexHome(model);
			PagingInfo pInfo = new PagingInfo();
			pInfo.setCurrentPage(p);
			pInfo.setSizePerPage(7);
			List<TruyenMa> lstTruyen = null;
			if(typeSearch.equals("tTenTruyen")){
				lstTruyen = truyenMaService.getLstByPropertyWithPaging("alias", k, "id", pInfo);
			}else if(typeSearch.equals("tTheLoai")){
				lstTruyen = truyenMaService.getLstByPropertyWithPaging("categories", k, "id", pInfo);
			}else{
				lstTruyen = truyenMaService.getLstByPropertyWithPaging("tacGia", k, "id", pInfo);
			}
			model.addAttribute("lstTruyen", lstTruyen);
			model.addAttribute("pInfo", pInfo);
			model.addAttribute("countRecord", pInfo.getCount());
			return "/truyenMa/search/index";
		}else{
			return "redirect:trang-chu.htm";
		}
	}
	
	//cac chuong moi cap nhat
	@RequestMapping("/chuong-moi-cap-nhat-trang-{curPage}.htm")
	public String lstNewChapter(Model model, @PathVariable("curPage") int curPage){
		sideBarIndexHome(model);
		PagingInfo pInfo = new PagingInfo();
		pInfo.setCurrentPage(curPage);
		List<ChapterTmpTruyenMa> lstChapterTruyen = chapterTmpTruyenMaService.getLstByOrderBy("id",pInfo);
		model.addAttribute("lstChapterTruyen", lstChapterTruyen);
		model.addAttribute("pInfo", pInfo);
		model.addAttribute("menuNav", "newChapter");
		return "/truyenMa/lstNewChapter/index";
	}
	
	//danh sach tac gia
		@RequestMapping("/danh-sach-tac-gia-trang-{curPage}.htm")
		public String lstAuthors(Model model, @PathVariable("curPage") int curPage){
			sideBarIndexHome(model);
			PagingInfo pInfo = new PagingInfo();
			pInfo.setCurrentPage(curPage);
			List<TacGia> lstAuthor = tacGiaService.getLstByLoaiTruyen(3,pInfo);
			model.addAttribute("lstAuthor", lstAuthor);
			model.addAttribute("pInfo", pInfo);
			return "/truyenMa/lstAuthors/index";
		}
	
}
